package com.zhiqiang.gameserver.thread;

import com.zhiqiang.gameserver.entity.ZQGameServerEntity;
import com.zhiqiang.gameserver.netty.NettyGameServer;
import com.zhiqiang.gameserver.queue.BaseQueue;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import java.util.concurrent.TimeUnit;

public abstract class AbstractThread implements Runnable{
    private static Logger log= LogManager.getLogger(AbstractThread.class.getName());

    private BaseQueue<ZQGameServerEntity> INSTANCE;

    public AbstractThread(BaseQueue<ZQGameServerEntity> INSTANCE) {
        this.INSTANCE = INSTANCE;
    }

    @Override
    public void run() {
        /**
         * 管他妈的3721，先来个死循环
         */
        while(true){
            try {
                ZQGameServerEntity entity = null;
                while (INSTANCE.getQueueSize() > 0) {
                    entity = INSTANCE.take();
                    if (null == entity) {
                    }else {
                        handleMsg(entity);
                    }
                }
                TimeUnit.MILLISECONDS.sleep(50);// 如果已经取完则让给其他线程一些时间片
            } catch (Exception e) {
                e.printStackTrace();
                log.error(e.getMessage());
            }
        }
    }

    public abstract void handleMsg(ZQGameServerEntity entity);
}
